package com.example.demo.controller;

import com.example.demo.exception.MyUncheckedException;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.dto.QuestionDTO;
import com.example.demo.pojo.entity.QuestionBank;
import com.example.demo.pojo.entity.QuestionFile;
import com.example.demo.service.IQuestionFileService;
import com.example.demo.service.imply.IBankServiceImply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/questionfile")
@Slf4j
@Api(tags = "题目与文件关联接口")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080"}, allowCredentials = "true")
public class QuestionFileController {
    private  final IQuestionFileService questionFileService;
    @ApiOperation("建立题目")
    @PostMapping("/createQuestion")
    public Result createQuestion(QuestionDTO questionDTO){
        System.out.println(questionDTO);
        int result=questionFileService.createQuestion(questionDTO);
        if(result!=-1){
            return Result.success(result);
        }
        return Result.error("插入失败");
    }
    @ApiOperation("更改题目位置")
    @PostMapping("/shift")
    public Result shiftQuestion(int questionId, int presentFileId, int goalFileId) throws MyUncheckedException {
        System.out.println("准备更新");
        System.out.println(questionId+"+"+presentFileId+"+"+goalFileId);
        QuestionFile result=questionFileService.shiftQuestion(questionId,presentFileId,goalFileId);
        if(result!=null){
            return Result.success(result);
        }
        return Result.error("更新失败");
    }
    @ApiOperation("复制题目位置")
    @PostMapping("/copy")
    public Result copyQuestion(int questionId,int presentFileId,int goalFileId) throws MyUncheckedException {
        System.out.println("准备复制");
        QuestionFile result=questionFileService.copyQuestion(questionId,presentFileId,goalFileId);
        if(result!=null){
            return Result.success(result);
        }
        return Result.error("更新失败");
    }
    @ApiOperation("在文件下删除题目")
    @PostMapping("/delete")
    public Result deleteQuestion(int questionId, int fileId) throws MyUncheckedException {
        System.out.println("准备更新");
        int result=questionFileService.deleteQuestion(questionId,fileId);
        if(result==1){
            return Result.success();
        }
        return Result.error("删除失败");
    }
    @ApiOperation("更新题目信息")
    @PostMapping("/updateQuestion")
    public Result updateQuestion(int questionId,QuestionDTO questionDTO){
        System.out.println("准备更新");
        System.out.println(questionDTO);
        QuestionBank result=questionFileService.updateQuestion(questionId,questionDTO);
        if(result!=null){
            return Result.success(result);
        }
        return Result.error("更新失败");
    }
}
